﻿#region Copyright and License
/*==============================================================================
 *  Copyright (c) cndotnet.org Corporation.  All rights reserved.
 * ===============================================================================
 * This code and information is provided "as is" without warranty of any kind,
 * either expressed or implied, including but not limited to the implied warranties
 * of merchantability and fitness for a particular purpose.
 * ===============================================================================
 * Licensed under the GNU General Public License (GPL) v2
 * http://www.cndotnet.org/ezsocio
 * ==============================================================================*/
#endregion

#region Using

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CNdotnet.Ezsocio.DataEntities;
using CNdotnet.Ezsocio.DataProvider;
using CNdotnet.Ezsocio.Core.Repository;

#endregion

namespace CNdotnet.Ezsocio.SQLServerProvider
{
    public class MessageRepository : RepositoryBase, IMessageRepository
    {
        #region Need refacting
        /// <summary>
        /// get message of inbox
        /// </summary>
        /// <param name="userID">user id</param>
        /// <returns></returns>
        public List<Message> GetInBoxMessages(int userID) {
            using (RepositoryContext db = new RepositoryContext()) {
                var q = from m in db.GetTable<Message>().OrderByDescending(msg => msg.SentDate)
                        where m.ToUserID == userID
                        select m;
                return q.ToList<Message>();
            }
        }
        /// <summary>
        /// change status of message
        /// </summary>
        /// <param name="msgID">message id</param>
        /// <param name="status">value of status</param>
        public  void ChangeStatus(int msgID, bool isNew) {
            using (RepositoryContext db = new RepositoryContext()) {
                var q = (from m in db.GetTable<Message>()
                        where m.ID == msgID
                        select m).SingleOrDefault<Message>();
                q.IsNew = isNew;
                db.SubmitChanges();
            }
        }
        /// <summary>
        /// delete message 
        /// </summary>
        /// <param name="msgID">message id</param>
        public void Delete(int msgID) {
            using (RepositoryContext db = new RepositoryContext()) {
                var q = (from m in db.GetTable<Message>()
                         where m.ID == msgID
                         select m).SingleOrDefault<Message>();
                db.GetTable<Message>().DeleteOnSubmit(q);
                db.SubmitChanges();
            }
        }

        #endregion

        public Message Insert(Message message)
        {
            InsertEntity(message);
            return message;
        }
    }
}
